<template>
  <view>
    <!-- 头部导航栏 -->
    <view class="navbar" :style="{ paddingTop: menu.top + 'px' }">
      <text class="title">登录</text>
    </view>
    <view class="passInput">
      <input type="text" placeholder="请输入账号" name="name" v-model="userName" />
    </view>
    <view class="passInput">
      <input
        type="password"
        placeholder="请输入密码"
        name="password"
        v-model="password"
        class="passHeight"
      />
    </view>
    <view>
      <button @click="onLogin">登录</button>
    </view>
  </view>

  <view>
    <button open-type="getPhoneNumber" @getphonenumber="onGetphonenumber">
      <text>手机号快捷登录</text>
    </button>
  </view>
</template>
<script setup lang="ts">
import { useMemberStore } from "@/stores";
import { getLoginAPI,getSocialLoginAPI } from "@/services/login";
import { ref } from "vue";
import { onLoad,onShareAppMessage,onShareTimeline } from '@dcloudio/uni-app'
const menu = ref();
menu.value = uni.getMenuButtonBoundingClientRect();
let userName = "";
let password = "";
let code = ''
// 登录
const onLogin = async () => {
  // uni.getUserInfo({
  //   provider: 'weixin',
  //   success: (res) => {
  //     console.log(res);

  //   },
  //   fail: (error) => {}
  // })
  const data = {
    username: "admin",
    password: "admin123",
  };
  const res = await getLoginAPI(data);
  loginSuccess(res.data);
};
// 登录获取手机号方法 参数：1.code 2.encryptedData 3.iv
const onGetphonenumber: UniHelper.ButtonOnGetphonenumber =async (ev) => {

  const encryptedData = ev.detail.encryptedData;
  const iv = ev.detail.iv;
  const res = await getSocialLoginAPI({
    code:ev.detail.code,
    encryptedData:encryptedData,
    iv:iv
  })
  console.log(res);
  const bearer = 'Bearer '
    uni.setStorageSync('appIdKey', res.data.userId);
    uni.setStorageSync('accessToken',bearer+res.data.accessToken)
   uni.switchTab({ url: "/pages/profile/profile" });

};
// 登录成功
const loginSuccess = (profile: any) => {
  // 保存信息
  const memberStore = useMemberStore();
  memberStore.setProfile(profile);
  console.log(profile);

  uni.showToast({
    title: "登录成功",
    icon: "success",
  });
  setTimeout(() => {
    uni.switchTab({ url: "/pages/profile/profile" });
  }, 500);
};

onLoad(async ()=>{
  // 获取参数code
  const res = await wx.login()
  code = res.code
})
</script>
<style lang="scss">
.navbar {
  margin-top: 8rpx;
  background-color: #fff;
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-bottom: 47rpx;
  .title {
    text-align: center;
    font-family: PingFang SC;
    font-weight: 600;
    font-size: 36rpx;
    color: #000000;
  }
}
.passInput {
  margin-left: 28rpx;
}
</style>
